package cn.chengpengper.rpc.registry.api;

import cn.chengpengper.rpc.protocol.meta.ServiceMeta;
import cn.chengpengper.rpc.registry.api.config.RegistryConfig;
import cn.chengpengper.rpc.spi.annotation.Spi;

import java.io.IOException;
import java.util.List;

/**
 * @author chengpeng.hu
 */
@Spi
public interface RegistryService {

    /**
     * 服务注册
     *
     * @param serviceMeta 服务元数据
     * @throws Exception 异常
     */
    void register(ServiceMeta serviceMeta) throws Exception;

    /**
     * 服务取消注册
     *
     * @param serviceMeta 服务元数据
     * @throws Exception 异常
     */
    void unRegister(ServiceMeta serviceMeta) throws Exception;

    /**
     * 服务发现
     *
     * @param serviceName     服务名称
     * @param invokerHashCode HashCode值
     * @param sourceIp        源地址IP
     * @return 服务元数据
     * @throws Exception 异常
     */
    ServiceMeta discovery(String serviceName, int invokerHashCode, String sourceIp) throws Exception;

    ;

    /**
     * 服务销毁
     *
     * @throws IOException IO异常
     */
    void destroy() throws IOException;

    ServiceMeta select(List<ServiceMeta> serviceMetaList, int invokerHashCode, String sourceIp);


    List<ServiceMeta> discoveryAll() throws Exception;
    /**
     * 默认服务初始化
     *
     * @param registryConfig 注册中心配置
     * @throws Exception 异常
     */
    default void init(RegistryConfig registryConfig) throws Exception {

    }
}
